<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>歌单编辑</title>
    <link rel="stylesheet" href="../res/layui/css/layui.css">
</head>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>歌单编辑</legend>
        <div class="layui-field-box">
            <div class="layui-col-md6">
                <form class="layui-form layui-form-pane" lay-filter="formMusicList" id="formMusicList">
                    <input type="hidden" name="id">
                    <input type="hidden" name="uid" value="1">
                    <div class="layui-form-item">
                        <img class="layui-upload-img" id="ID-upload-demo-img"
                             style="width:92px;height: 92px;border: 1px solid rgba(0,0,0,0.1)">
                        <input type="hidden" name="pic" id="picUrl">
                    </div>
                    <!--表单控件项：文件上传（歌曲文件）-->
                    <div class="layui-form-item">
                        <button class="layui-btn layui-btn-normal" type="button" id="btnMusicList">
                            <i class="layui-icon layui-icon-upload"></i>
                            选择封面图
                        </button>
                    </div>
                    <!--表单控件项：歌单名-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">歌单名:</label>
                        <div class="layui-input-block">
                            <input class="layui-input" type="text" name="name" id="musiclistname" lay-verify="required"
                                   lay-verType="tips" lay-reqText="请输入歌单名称">
                        </div>
                    </div>
                    <!--表单控件项：标签-->
                    <div class="layui-form-item">
                        <label class="layui-form-label">标签:</label>
                        <div class="layui-input-block">
                            <input class="layui-input" type="text" name="tags" id="tags"
                                   placeholder="多个标签之间使用逗号(,)分隔" lay-verify="required" lay-verType="tips"
                                   lay-reqText="请输入至少一个标签">
                        </div>
                    </div>
                    <!-- 提交按钮    -->
                    <div class="layui-form-item" style="text-align: right">
                        <button type="button" class="layui-btn layui-btn-warm" lay-submit id="updateMusicList">确认修改
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </fieldset>
</div>
<script src="../res/layui/layui.js"></script>
<script>
    layui.use(function () {
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;
        //获取文件上传组件
        var upload = layui.upload;
        
        //文件上传
        upload.render({
            elem: '#btnMusicList', //绑定元素
            url: '/file/upload', //上传接口
            field: 'file', //文件域字段名
            accept: 'images', //只允许上传图片
            acceptMime: 'image/*', //只显示图片文件
            done: function (res) {
                //上传成功后的回调
                if (res.code === 0) {
                    //设置图片预览
                    $('#ID-upload-demo-img').attr('src', res.data);
                    //设置隐藏字段的值
                    $('#picUrl').val(res.data);
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            },
            error: function () {
                //请求异常回调
                layer.msg('上传失败', {icon: 2});
            }
        });
        
        // 表单提交
        form.on('submit(updateMusicList)', function(data){
            var formData = data.field;
            // 发送更新请求
            $.ajax({
                url: '/musicList/update',
                type: 'PUT',
                contentType: 'application/json',
                data: JSON.stringify(formData),
                success: function(res) {
                    if (res.code === 0) {
                        layer.msg('修改成功', {icon: 1});
                        // 关闭当前窗口
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        // 刷新父页面表格
                        parent.location.reload();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function() {
                    layer.msg('请求失败', {icon: 2});
                }
            });
            return false; //阻止表单跳转
        });
    });
</script>
</body>
</html>